﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Collections;
using WXYW.Model;

namespace WXYW
{
    public partial class ManageComponent : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString[Constants.QUERYSTR_PAGE_NO]))
                {
                    AccessoryListGridView.PageIndex = Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_PAGE_NO]);
                }
                BindAccessoryGridView();
            }
        }

        protected void AccessoryListGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Transfer"))
            {
                HideTransferAccessoryButton.Value = e.CommandArgument.ToString();
                AccessoryService accessoryService = new AccessoryService();
                Accessory accessoryEntity = accessoryService.GetAccessoryByID(Convert.ToInt32(e.CommandArgument));
                TransferAccessoryMaterialNOTextBox.Text = accessoryEntity.MaterialNO;
                TransferAccessoryModelNOTextBox.Text = accessoryEntity.ModelNO;
                TransferAccessoryDescriptionTextBox.Text = accessoryEntity.Description;
                TransferAccessoryCountTextBox.Text = accessoryEntity.Count.ToString();

                TransferCountTextBox.Focus();
                TransferAccessoryModalPopupExtender.Show();
            }
            else if (e.CommandName.Equals("DeleteA"))
            {
                AccessoryService accessoryService = new AccessoryService();
                accessoryService.DeleteAccessory(e.CommandArgument.ToString());
                BindAccessoryGridView();
            }
            else if (e.CommandName.Equals("Edit"))
            {
                Response.Redirect(string.Format("EditAccessory.aspx?{0}={1}&{2}={3}", Constants.QUERYSTR_DB_ID, e.CommandArgument.ToString(),
                    Constants.QUERYSTR_PAGE_NO, AccessoryListGridView.PageIndex));
            }
        }

        protected void AccessoryListGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }

            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= AccessoryListGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (AccessoryListGridView.PageIndex + 1).ToString();
            }
        }

        protected void SaveTransferAccessoryButton_Click(object sender, EventArgs e)
        {
            try
            {
                AccessoryService accessoryService = new AccessoryService();
                accessoryService.TransferAccessory(Convert.ToInt64(HideTransferAccessoryButton.Value), Convert.ToInt32(TransferCountTextBox.Text),
                    "", Convert.ToInt32(TransferAccessoryStationsDropDownList.SelectedValue), ContextService.CurrentAccount);
                MsgLbl.Text = "转储成功。";
                BindAccessoryGridView();
            }
            catch (Exception ex)
            {
                MsgLbl.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        private void BindAccessoryGridView()
        {
            AccessoryService accessoryService = new AccessoryService();
            AccessoryListGridView.DataSource = accessoryService.GetAccessoryList(keyWordTextBox.Text);
            AccessoryListGridView.DataBind();
        }

        protected void TransferAccessoryPanel_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ServiceStationService stationService = new ServiceStationService();
                TransferAccessoryStationsDropDownList.DataSource = stationService.GetALLServiceStations();
                TransferAccessoryStationsDropDownList.DataTextField = "Name";
                TransferAccessoryStationsDropDownList.DataValueField = "ID";
                TransferAccessoryStationsDropDownList.DataBind();
            }
        }

        protected void AccessoryListGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < AccessoryListGridView.PageCount)
            {
                AccessoryListGridView.PageIndex = e.NewPageIndex;
            }
            
            BindAccessoryGridView();
        }

        protected void searchLinkButton_Click(object sender, EventArgs e)
        {
            BindAccessoryGridView();
        }

        protected void resetLinkButton_Click(object sender, EventArgs e)
        {
            keyWordTextBox.Text = "";
            BindAccessoryGridView();
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<AccessoryModel> dds = new List<AccessoryModel>();
            dds.Add(new AccessoryModel());
            return dds;
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            AccessoryListGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindAccessoryGridView();
        }

        protected void AccessoryListGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            AccessoryService accessoryService = new AccessoryService();
            IQueryable<AccessoryModel> products = accessoryService.GetAccessoryList(keyWordTextBox.Text).AsQueryable();
            RecordsNumber = products.Count();

            string sortDirection = "asc";
            if (Session["OrderDirection"] != null)
            {
                sortDirection = Session["OrderDirection"].ToString();
                if (sortDirection.Equals("asc"))
                {
                    Session["OrderDirection"] = "desc";
                }
                else
                {
                    Session["OrderDirection"] = "asc";
                }
            }
            else
            {
                Session["OrderDirection"] = "asc";
            }

            AccessoryListGridView.DataSource = products.OrderBy(string.Format("{0} {1}", e.SortExpression, sortDirection)).ToList();
            AccessoryListGridView.DataBind();
        }
    }
}
